package xyz.chaobei.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.chaobei.entity.OrgPermissionEntity;
import xyz.chaobei.service.OrgPermissionService;
import xyz.chaobei.util.JsonUtil;

import java.util.List;

/**
 * 前端控制器
 *
 * @author mrc
 */
@RestController
@RequestMapping("api/orgPermission")
public class OrgPermissionController {

    @Autowired
    private OrgPermissionService orgPermissionService;

    @GetMapping("/")
    public JSONObject getTree() {
        List<OrgPermissionEntity> treeEntities = orgPermissionService.findTree();
        return JsonUtil.getSuccess(treeEntities);
    }

    /**
     * 修改权限信息
     *
     * @return
     */
    @PutMapping("/{id}")
    public JSONObject update(@RequestBody OrgPermissionEntity entity, @PathVariable("id") Integer id) {

        entity.setId(id);
        boolean isUpdate = orgPermissionService.updateById(entity);
        return JsonUtil.getSuccess(isUpdate);
    }

    /**
     * 修改菜单的排序
     * @param index
     * @param id
     * @return
     */
    @PutMapping("/index/{id}")
    public JSONObject updateIndex(@RequestBody Integer index, @PathVariable("id") Integer id) {
        boolean isUpdate = orgPermissionService.updateIndex(index, id);
        return JsonUtil.getSuccess(isUpdate);
    }

    /**
     * 删除一个元素
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JSONObject deleteById(@PathVariable("id") Integer id) {

        boolean isUpdate = orgPermissionService.removeById(id);
        return JsonUtil.getResult(isUpdate);
    }

    /**
     * 获得权限信息
     *
     * @return
     */
    @GetMapping("/{id}")
    public JSONObject getPermission(@PathVariable("id") Integer id) {
        OrgPermissionEntity entity = orgPermissionService.getById(id);
        return JsonUtil.getSuccess(entity);
    }

    @PostMapping("/")
    public JSONObject save(@RequestBody OrgPermissionEntity entity) {
        boolean isSave = orgPermissionService.save(entity);
        return JsonUtil.getSuccess(isSave);
    }

}
